/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.snda.lidejia.hivetotalsort.mr;

import com.snda.lidejia.hivetotalsort.type.HiveType;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;

/**
 *
 * @author lidejia
 */
public class SortMeta {

    private int index;
    private String type;
    private String order;

    public SortMeta(int index, String type, String order) {
        this.index = index - 1;
        this.type = type;
        this.order = order;
    }

    public int getIndex() {
        return index;
    }

    public HiveType parse(String[] tokens) {
        WritableComparable value = null;
        if (type.equalsIgnoreCase("int")) {
            value = new IntWritable(Integer.parseInt(tokens[index]));
        } else if (type.equalsIgnoreCase("long")) {
            value = new LongWritable(Long.parseLong(tokens[index]));
        } else if (type.equalsIgnoreCase("string")) {
            value = new Text(tokens[index]);
        }
        HiveType hiveValue = null;
        if (order.equalsIgnoreCase("desc")) {
            hiveValue = new HiveType(value, true);
        } else if (order.equalsIgnoreCase("asc")) {
            hiveValue = new HiveType(value, false);
        }
        return hiveValue;
    }
}
